package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.conclusion;

import com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.addprem.AddPremStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * description:
 *
 * @author jackdaw
 * @date 2020-06-29 14:31
 */
public class ConclusionStrategyFactory {
    private static Logger logger = LoggerFactory.getLogger(ConclusionStrategyFactory.class);

    private static ConcurrentHashMap<String, ConclusionStrategy> conclusionStrategyMap = new ConcurrentHashMap<>();

    static {
        try {
            Resource resource = new ClassPathResource("conclusionstrategy.properties");
            InputStream inputStream = resource.getInputStream();
            Properties properties = new Properties();
            properties.load(inputStream);
            for (String key : properties.stringPropertyNames()) {
                ConclusionStrategy strategy = (ConclusionStrategy) Class.forName(properties.getProperty(key)).newInstance();
                Arrays.stream(key.split(",")).forEach(s -> {
                    conclusionStrategyMap.put(s, strategy);
                    logger.info("addPremMap add key: " + s + ", new instance:" + strategy);
                });
            }
        } catch (IOException | IllegalAccessException | InstantiationException | ClassNotFoundException e) {
            logger.error(e.getMessage());
        }
    }

    public static ConclusionStrategy getStrategy(String key) {
        return conclusionStrategyMap.get(key);
    }

    public static Set<String> getKeys() {
        return conclusionStrategyMap.keySet();
    }
}
